package com.yaorange.jk.web;

import com.yaorange.jk.entity.Contract;
import com.yaorange.jk.entity.User;
import com.yaorange.jk.service.ContractService;
import com.yaorange.jk.service.UserService;
import com.yaorange.jk.utils.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class SendMail {

    private String DEFAULT_EMAIL = "";
    @Autowired
    private ContractService contractService;

    @Autowired
    private UserService userService;

    public void sendMail() {
        System.out.println("正在发送邮件..." + new Date());
        String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<Contract> li = contractService.findByDeliveryPeriod(now);
        if (li != null && li.size() != 0) {
            for (int i = 0; i < li.size(); i++) {
                String createBy = li.get(i).getCreateBy();
                User user = userService.findById(createBy);
                String email = user.getUserInfo().getEmail();
                if (email == null || "".equals(email)) {
                    email = DEFAULT_EMAIL;
                }
                MailUtils.sendMail(email, user.getUserName() + ":你好，公司重要通知！",
                        li.get(i).getContractNo() + "订单已经到了交货日期，请及时处理！");
                System.out.println("邮件发送成功");
            }
        } else {
            System.out.println("---》》》今天还没有 购销合同 需要交货！《《《---");
        }

    }

    public void setDEFAULT_EMAIL(String DEFAULT_EMAIL) {
        this.DEFAULT_EMAIL = DEFAULT_EMAIL;
    }
}
